package cn.pku.eecs.ldn.activity;

import cn.pku.eecs.ldn.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

public class TestAnimation1Activity extends Activity {
    
    Sprite sprite;
    
    @Override       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        sprite = new Sprite(this);
        setContentView(sprite);
    }
    
    class Sprite extends ImageView {
    
        Bitmap bitmap;
        Paint paint;
        RotateAnimation rotate;
        AlphaAnimation blend;
        ScaleAnimation scale;
        AnimationSet spriteAnimation;
    
        float centerX;
        float centerY;
        float offsetX;
        float offsetY;
    
        public Sprite(Context context) {
            super(context);
    
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.naeun100);
            offsetX = bitmap.getWidth() / 2;
            offsetY = bitmap.getHeight() / 2;
    
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setFilterBitmap(true);
        }
    
        @Override           
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            if (spriteAnimation == null) {
                centerX = canvas.getWidth() / 2;
                centerY = canvas.getHeight() / 2;
                createAnimation(canvas);
            }
            paint.setColor(Color.RED);
            canvas.drawRect(new Rect(0, 0, canvas.getWidth(), canvas.getHeight()), paint);
            canvas.drawBitmap(bitmap, centerX - offsetX, centerY - offsetY, paint);
        }
    
        private void createAnimation(final Canvas canvas) {
    
            rotate = new RotateAnimation(0, 360, centerX, centerY);
            rotate.setRepeatMode(Animation.REVERSE);
            rotate.setRepeatCount(Animation.INFINITE);
            scale = new ScaleAnimation(0, 2, 0, 2, centerX, centerY);
            scale.setRepeatMode(Animation.REVERSE);
            scale.setRepeatCount(Animation.INFINITE);
            scale.setInterpolator(new AccelerateDecelerateInterpolator());
    
            spriteAnimation = new AnimationSet(true);
            spriteAnimation.addAnimation(rotate);
            spriteAnimation.addAnimation(scale);
            spriteAnimation.setDuration(10000L);
    
            startAnimation(spriteAnimation);
    
        }
      }
    }  
